{extend name="layout:base" /}

{block name="content"}
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="test-table-reload-btn">
                <div class="layui-inline">
                    <select name="order" id="order" lay-filter="order">
                        <option value="">排序规则</option>
                        <option value="money_asc">余额升序</option>
                        <option value="money_desc">余额降序</option>
                        <option value="moneypay_asc">消费升序</option>
                        <option value="moneypay_desc">消费降序</option>
                    </select>
                </div>
                <div class="layui-inline">
                    <input class="layui-input" name="keywords" id="keywords" autocomplete="off">
                </div>
                <button class="layui-btn" data-type="reload">搜索</button>
            </div>


        </div>
        <div class="layui-card-body">
            <table id="tbList" class="layui-table" lay-filter="tbList" ></table>
            <script type="text/html" id="toolbarDemo">
                <div class="layui-btn-container"></div>
            </script>
            <script type="text/html" id="state">

                <input type="checkbox" name="state" value="{{d.id}}" lay-skin="switch" lay-text="正常|停用" lay-filter="state" {{ d.state== 3 ? 'checked' : '' }}
                {if condition="$Think.session.admin.levelNum ==1"}disabled{/if} >

            </script>
            <script type="text/html" id="op">
                <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
                <a class="layui-btn layui-btn-xs" lay-text="‘{{d.name}}’报表" lay-href="{:url(('/admin/web/index'))}?userid={{d.id}}">
                    报表
                    {{#  if(d.keystate >0){ }}
                    <span style="color:#F00">({d.keystate}>)</span>
                    {{#  } }}
                </a>
                {if condition="$Think.session.admin.levelNum >1"}
                <a class="layui-btn layui-btn-xs" lay-event="user_money">资金</a>
                {/if}

            </script>
        </div>
    </div>
</div>

{/block}

{block name="script"}
<script>
    layui.config({
        base: '__STATIC__/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table', 'util','form'], function () {

        //表格
        var $ = layui.$,form = layui.form,layer = layui.layer,table = layui.table;
        table.render({
            elem: '#tbList',
            url: "{:url('getList')}",
            where:{usertype:'qf'},
            toolbar: '#toolbarDemo',
            cellMinWidth: 80,
            cols: [[
                {field: 'id', fixed: 'left', title: 'ID',width:66, sort: false},
                {field: 'name',fixed: 'left',title: '客户名称'},
                {field: 'money_sc',fixed: 'left',title: '首次充值',width:100},
                {field: 'money',fixed: 'left',title: '账户余额',width:100},
                {field: 'webNum',  title: '网站',width:80},
                {field: 'keyNum',  title: '关键词',width:80},
                {field: 'moneypay',  title: '消费统计',width:110,sort:true},
                {field: 'intime',  title: '录入时间',width:160,sort:true},
                {field: 'content',  title: '备注',width:160},
                {field: 'state', title: '状态',width:90, templet: '#state'},
                {fixed: 'right', title: '操作',width:150, toolbar: '#op'}
            ]],
            page: true,
            limit:15
        });

        // 监听工具条
        table.on('tool(tbList)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                var url = "{:url('edit')}?id=" + data['id'];
                var index = layer.open({
                    type: 2,
                    title: '修改内容',
                    area: ['800px', '600px'],
                    shade: 0,
                    maxmin: true,
                    content: url,
                    zIndex: layer.zIndex
                });
                layer.full(index);
            }else if (obj.event === 'user_money') {
                var url = "{:url('/admin/money/user')}?userID=" + data['id'];
                var index = layer.open({
                    type: 2,
                    title: '用户资金',
                    area: ['90%', '90%'],
                    shade: 0,
                    maxmin: true,
                    content: url,
                    zIndex: layer.zIndex
                });
            }
        });
        //监听单元格编辑
        table.on('edit(tbList)', function (obj) {
            var value = obj.value; //得到修改后的值
            var data = obj.data; //得到所在行所有键值
            var field = obj.field; //得到字段
            layer.msg('[ID: ' + data.id + '] ' + field + ' 字段更改为：' + value);

            $.post("{:url('edit')}", {id: data.id, sort: value}, function (result) {
                if (result['code'] == 0) {
                    layer.msg(result['msg'], {icon: 1, time: 1000}, function () {
                        location.reload();
                    });
                } else {
                    layer.msg(result['msg'], {icon: 3, time: 3000});
                }
            });
        });


        //监听推荐
        form.on('switch(state)', function (data) {
            //停用
            var id=data.value;
            layer.open({
                content: '确认将该客户停用(该客户下的所有站点都将停用)?'
                ,btn: ['确定', '取消']
                ,yes: function(index, layero){
                    $.post("{:url('stop_user')}",{id:id}, function (result) {
                        if (result['code'] == 0) {
                            location.reload();
                        } else {
                            layer.msg(result['msg'], {icon: 3, time: 3000});
                        }
                    });
                    form.render();
                    layer.close(index);
                }
                ,btn2: function(index, layero){
                    layer.close(index);
                }
            });
            return false;



        });


        //操作
        var $ = layui.$, active = {
            add: function () {
                var index = layer.open({
                    type: 2,
                    title: '添加内容',
                    area: ['800px', '600px'],
                    shade: 0,
                    maxmin: true,
                    content: "{:url('add')}",
                    zIndex: layer.zIndex
                });
                layer.full(index);
            },
            reload: function(){
                var order=$('#order').val();
                var pid=$('#pid').val();
                var cid=$('#cid').val();
                var keywords=$('#keywords').val();

                //执行重载
                table.reload('tbList', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: {
                        pid: pid
                        ,cid: cid
                        ,order: order
                        ,keywords: keywords
                    }
                });

            }
        };

        // $("#btnAdd").on('click', function () {
        //     var type = $(this).data('type');
        //     active[type] ? active[type].call(this) : '';
        // });

        $('.test-table-reload-btn .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

    });

</script>
{/block}